package com.etheart.lyric.htmlparser.service.baidu;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Service;

import com.etheart.model.entity.Album;
import com.etheart.model.entity.Song;

@Service("htmlparse_BaiduAlbumParseService")
public class BaiduAlbumParseService {
	private static final Log logger = LogFactory
			.getLog(BaiduAlbumParseService.class);

	public Album getAlbum(Parser parser, String netid) {
		Album album = new Album();
		album.setNetId(netid);
		return getAlbum(parser, album);
	}

	private Album getAlbum(Parser parser, Album album) {
			// 得到专辑名称
			try {
				String albumName = parseAlbumName(parser);
				album.setAlbumname(albumName);
			} catch (Exception e) {
				logger.debug(album.getNetId()+":专辑名称转化错误");
			}
			
			// 得到专辑介绍
			try {
				String albumIntro = parseAlbumIntro(parser);
				album.setIntro(albumIntro);
			} catch (Exception e) {
				logger.debug(album.getNetId()+":专辑介绍转化错误");
			}
			
			// 得到专辑发行 语种 公司 发行时间 等
			String[] strs=null;
			try {
				strs = parseAlbumLanguage(parser);
				if (strs!=null&&strs.length>=3) {
					//album.setLanguage(strs[0]);
					album.setConame(strs[0]);
					album.setLanguage(strs[1]);
				}
			} catch (Exception e) {
				logger.debug(album.getNetId()+":语种转化错误");
			}
			
			try {
				album.setPublishtime(com.etheart.framework.core.util.DateUtils.parseDate(strs[2]));
			} catch (Exception e2) {
				logger.debug(album.getNetId()+":发行时间转化错误");
			}
			// 专辑图片
			try{
				String pic = parseAlbumImg(parser);
				album.setPicture(pic);
			} catch (Exception e2) {
				logger.debug(album.getNetId()+":专辑图片转化错误");
			}
	

		return album;
	}

	private String parseAlbumImg(Parser parser) throws ParserException {
		parser.reset();
		NodeFilter imgFilter = new AndFilter(new TagNameFilter("img"),new HasAttributeFilter("class", "cover-image lazyload")); 
		//<img src="http://pic.top100.cn/SpecialImg/33/middle_special_323142.jpg" width="190" height="190" />
		// 得到专辑图片地址
		NodeList nodeList = parser.parse(imgFilter);
		
			Tag tag = (Tag) nodeList.elementAt(0);
			String src = tag.getAttribute("org_src");
		return src;
	}

	private String[] parseAlbumLanguage(Parser parser) throws ParserException {
		parser.reset();
		String[] strs=new String[3];
		NodeFilter filter = new AndFilter(new TagNameFilter("dl"),new HasAttributeFilter("class","clearfix")) ;
		NodeList nodeList = parser.parse(filter);
		NodeList children  =  nodeList.elementAt(0).getChildren();
		//公司
		Node conode = children.elementAt(children.size()-2);
		strs[0]=conode.toPlainTextString().replace("所属公司：", "");
		//流派
		Node node2 = children.elementAt(children.size()-4);
		strs[1]=node2.toPlainTextString().replace("流派：", "");
		//时间
		Node node3 = children.elementAt(children.size()-6);
		strs[2]=node3.toPlainTextString().replace("发行时间：", "");
		return strs;
	}

	private String parseAlbumIntro(Parser parser) throws ParserException {
		parser.reset();
		//<p style="padding:0 10px; line-height:22px;">
		NodeFilter filter = new AndFilter(new TagNameFilter("span"),
				new HasAttributeFilter("class", "description"));
		NodeList nodeList = parser.parse(filter);
		if (nodeList.size() <= 0) {
			return null;
		}
		Node node =  nodeList.elementAt(0);
		String intro=node.toPlainTextString();
		return intro.equals("暂无介绍")?"":intro;
	}

	public List<Song> getSongs(Parser parser) throws ParserException {
		List<Song> songs = new ArrayList<Song>(10);
		parser.reset();
	//<ul class="top100list clearfix"   // style="width:663px;"
		NodeFilter filter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class","song-title"));
		NodeList nodeList = parser.parse(filter);
		for (int i = 0; i < nodeList.size();i++) {
			Node node = nodeList.elementAt(i);
			Node aNode = node.getChildren().elementAt(1);
			String html = aNode.toHtml();
			Tag tag = (Tag) aNode;
			String songName = tag.getAttribute("title");
			String songurl = tag.getAttribute("href");
			String songNetid = songurl.replace("/song/", "");
			Song song = new Song();
			song.setNetId(songNetid);
			song.setSongname(songName);
			song.setFromUrl(songurl);
			songs.add(song);
		}
		return songs;
	}

	private String parseAlbumName(Parser parser) throws ParserException {
		parser.reset();
		//<dd class="album-title">如风 Autumn Version (Autumn Version)</dd>

		NodeFilter filter = new AndFilter(new TagNameFilter("dd"),new HasAttributeFilter("class","album-title")); 
		NodeList nodeList = parser.parse(filter);
		Node nameNode = nodeList.elementAt(0);
		return nameNode.toPlainTextString();
	}
	
	
}
